package org.dromara.data.dnnyun.controller;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.data.dnnyun.service.IDnnyunDataService;
import org.dromara.third.dnnyun.domain.bo.DyConsumeOrderBo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.curd.dnnyun.domain.vo.DnnyunConsumeOrderVo;
import org.dromara.curd.dnnyun.domain.bo.DnnyunConsumeOrderBo;
import org.dromara.curd.dnnyun.service.IDnnyunConsumeOrderService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 东云消费订单
 *
 * @author wdm
 * @date 2024-02-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/data/dnnyun/consumeOrder")
public class DnnyunConsumeOrderController extends BaseController {

    private final IDnnyunConsumeOrderService dnnyunConsumeOrderService;
    private final IDnnyunDataService dnnyunDataService;

    /**
     * 查询东云消费订单列表
     */
    @SaCheckPermission("data:dnnyun:consumeOrder:list")
    @GetMapping("/list")
    public TableDataInfo<DnnyunConsumeOrderVo> list(DnnyunConsumeOrderBo bo, PageQuery pageQuery) {
        return dnnyunConsumeOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出东云消费订单列表
     */
    @SaCheckPermission("data:dnnyun:consumeOrder:export")
    @Log(title = "东云消费订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DnnyunConsumeOrderBo bo, HttpServletResponse response) {
        List<DnnyunConsumeOrderVo> list = dnnyunConsumeOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "东云消费订单", DnnyunConsumeOrderVo.class, response);
    }

    /**
     * 获取东云消费订单详细信息
     *
     * @param orderId 主键
     */
    @SaCheckPermission("data:dnnyun:consumeOrder:query")
    @GetMapping("/{orderId}")
    public R<DnnyunConsumeOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long orderId) {
        return R.ok(dnnyunConsumeOrderService.queryById(orderId));
    }

    /**
     * 新增东云消费订单
     */
    @SaCheckPermission("data:dnnyun:consumeOrder:add")
    @Log(title = "东云消费订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DnnyunConsumeOrderBo bo) {
        return toAjax(dnnyunConsumeOrderService.insertByBo(bo));
    }

    /**
     * 修改东云消费订单
     */
    @SaCheckPermission("data:dnnyun:consumeOrder:edit")
    @Log(title = "东云消费订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DnnyunConsumeOrderBo bo) {
        return toAjax(dnnyunConsumeOrderService.updateByBo(bo));
    }

    /**
     * 删除东云消费订单
     *
     * @param orderIds 主键串

    @SaCheckPermission("data:dnnyun:consumeOrder:remove")
    @Log(title = "东云消费订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] orderIds) {
        return toAjax(dnnyunConsumeOrderService.deleteWithValidByIds(List.of(orderIds), true));
    }
     */
    /**
     * 同步东云消费订单
     */
    @SaCheckPermission("data:dnnyun:consumeOrder:sync")
    @GetMapping("/sync")
    public R<Void> sync(DnnyunConsumeOrderBo dnnyunConsumeOrderBo) {
        DyConsumeOrderBo dyConsumeOrderBo = new DyConsumeOrderBo();
        Map<String, Object> params = dnnyunConsumeOrderBo.getParams();
        if(null != params.get("beginOrderTime") && null != params.get("endOrderTime")){
            dyConsumeOrderBo.setOrderTimeBegin((String) params.get("beginOrderTime"));
            dyConsumeOrderBo.setOrderTimeEnd((String) params.get("endOrderTime"));
        }
        dnnyunDataService.syncConsumeOrder(dyConsumeOrderBo);
        return R.ok();
    }
}
